package com.yunshipei.utils;

import android.util.Log;

import com.iflytek.base.speech.adapter.Error;
import com.iflytek.base.speech.adapter.SpeechSynthesizerExt;
import com.iflytek.base.speech.adapter.TtsListener;
import com.yunshipei.EnterApplication;

/**
 * Created by cike on 15/12/10.
 */
public class TtsUtil {
    private static final String TAG = TtsUtil.class.getSimpleName();
    private static TtsUtil ttsUtil = null;
    //语音合成对象
    private SpeechSynthesizerExt mTts;

    /** TTS引擎 - 本地TTS   */
    public static final String TTS_ENGINE_TYPE_LOCAL = "local";
    /** TTS引擎 - 网络TTS   */
    public static final String TTS_ENGINE_TYPE_CLOUD = "cloud";
    /** 网络TTS发音人角色- （青年女声）  用于intp65引擎          默认   */
    public static final String CLOUD_TTS_ROLE_XIAOYAN = "xiaoyan";
    /** 本地TTS小燕（女声）发音人 */
    public static final String LOCAL_TTS_ROLE_XIAOYAN = "xiaoyan";
    // 引擎类型
    private String mEngineType = TTS_ENGINE_TYPE_LOCAL;
    // 网络合成发音人
    private String cloudVoiceRole = CLOUD_TTS_ROLE_XIAOYAN;

    // 本地合成发音人
    private String localVoiceRole = LOCAL_TTS_ROLE_XIAOYAN;

    private int cloudRoleSelectedNum = 0;
    private int localRoleSelectedNum = 0;

    private String[] cloudVoicersEntries;
    private String[] cloudVoicersValue ;

    private String[] localVoicersEntries;
    private String[] localVoicersValue ;
    private SpeechListener speechListener = null;

    // 播放进度
    private int mPercentForPlaying = 0;

    private TtsUtil() {
        // 初始化合成对象
        mTts = SpeechSynthesizerExt.getInstance(EnterApplication.getApp());
        mTts.initTtsEngine(mTtsListener, null);
        mEngineType = TTS_ENGINE_TYPE_LOCAL;
        setParam();
    }

    public static TtsUtil getInstance() {
        if (ttsUtil == null) {
            ttsUtil = new TtsUtil();
        }
        return ttsUtil;
    }

    /**
     * 开始合成
     * @param context
     */
    public void startSpeech(String context) {
        mTts.startSpeak(context, 3);
    }

    public void startSpeech(String context, SpeechListener speechListener) {
        this.speechListener = speechListener;
        startSpeech(context);
    }

    /**
     * 停止合成
     */
    public void stopSpeech() {
        mTts.stopSpeak();
    }

    /**
     * 取消合成
     */
    public void cancelSpeech() {
        mTts.cancelSpeak();
    }

    public void destory() {
        mTts.stopSpeak();
        mTts.destroy();
    }

    /**
     * 参数设置
     * @return
     */
    private void setParam(){
        //设置合成语速
        mTts.setSpeed(50);

        //设置合成音量
        mTts.setVolume(80);

        // 设置发音人
        if (mEngineType.equals(TTS_ENGINE_TYPE_LOCAL)) {
            mTts.setRole(localVoiceRole);
        }
/*        else {
            mTts.setRole(cloudVoiceRole);
        }*/

        //设置引擎类型
        mTts.setEngineType(mEngineType);
    }

    /**
     * 合成回调监听。
     */
    private TtsListener mTtsListener = new TtsListener() {
        @Override
        public void onProgress(int pos) {
            // 播放进度
            mPercentForPlaying = pos;
            Log.i(TAG, "onProgress pos is " + pos);
        }

        @Override
        public void onStart() {
            Log.d(TAG, "开始播放");
            if (speechListener != null) {
                speechListener.onStart();
            }
        }

        @Override
        public void onEnd() {
            Log.d(TAG, "播放完成");
            if (speechListener != null) {
                speechListener.onEnd();
            }
        }

        @Override
        public void onError(Error error) {
            Log.e(TAG, "播放出错" + error.code);
        }

        @Override
        public void onInited() {
            //初始化完成
            Log.d(this.getClass().getSimpleName(), "初始化完成");
        }
    };

    public interface SpeechListener{
        void onEnd();
        void onStart();
    }
}
